@extends('backstage.layout.master')
@section('title', '赠品优惠列表')
@section('style')
<link rel="stylesheet" href="backstage/assets/css/bootstrap-timepicker.css"/>
<link rel="stylesheet" href="backstage/assets/css/daterangepicker.css"/>
<link rel="stylesheet" href="backstage/assets/css/colorpicker.css"/>
<link rel="stylesheet" href="backstage/assets/css/datepicker.css"/>
@stop
@section('content')
    <style>
        .state li {
            list-style: none;
            float: left;
            padding: 0 20px;
            text-align: center;
            height: 40px;
            line-height: 40px;
        }

        .state ul {
            margin: 0;
        }

        .state a {
            font-size: 12px;
        }
        .che a {
            font-weight: bold;
            color: #151a7c;
        }
    </style>
    <div class="breadcrumbs" id="breadcrumbs">
        <script type="text/javascript">
            try {
                ace.settings.check('breadcrumbs', 'fixed')
            } catch (e) {
            }
        </script>

        <ul class="breadcrumb">
            <li>
                <i class="icon-home home-icon"></i>
                <a target="_blank" href="backstage/home">主页</a>
            </li>

            <li class="active">
                <a target="_blank" href="/backstage/gift/gift-list">赠品</a>
            </li>
            <li>
                赠品优惠范围列表
            </li>
        </ul><!-- .breadcrumb -->

        <div class="nav-search" id="nav-search">
        </div><!-- #nav-search -->
    </div>

    <div class="page-content">
        <div class="page-header">
            <h1>
                赠品
                <small>
                    <i class="icon-double-angle-right"></i>
                    赠品优惠范围列表
                </small>
            </h1>
        </div>
        <div class="row">
            <div class="col-sm-12">
                <h3 class="header smaller lighter red">赠品简介：</h3>
                <div class="well">
                    <h4 class="green smaller lighter">赠品参数</h4>
                    1、Id：{{isset($gift->id)?$gift->id:""}}</br>
                    2、赠品名：{{isset($gift->name)?$gift->name:""}}</br>
                    3、赠品简介：{{isset($gift->gift_desc)?$gift->gift_desc:""}}</br>
                    3、当前库存数：{{isset($gift->num)?$gift->num:0}}</br>
                    5、赠品有效开始参考时间：{{isset($gift->begin_time)?date('Y-m-d H:i:s',$gift->begin_time):""}}</br>
                    6、赠品有效结束参考时间：{{isset($gift->end_time)?date('Y-m-d H:i:s',$gift->end_time):""}}</br>
                </div>
            </div>
            <div class="col-xs-12">
                <!-- PAGE CONTENT BEGINS -->
                <div class="alert alert-info">
                    <i class="icon-hand-right"></i>
                    适用范围添加:&nbsp
                    <select id="sel_gift_type">
                        {{--<option value="sel_quota">满额减</option>--}}
                        {{--<option value="sel_prod_temp">产品模版</option>--}}
                        <option selected value="sel_prod">产品</option>
                        {{--<option value="sel_brand">品牌</option>--}}
                    </select>
                    <input type="text" id="sel_quota"  class="sel_gift_type" style="display: none">
                    <select id="sel_prod_temp"  class="sel_gift_type" style="display: none">
                        @if(sizeof($product_temps)==0)
                        <option value="">已经没有可添加适用范围</option>
                        @else
                            @foreach($product_temps as $temp)
                                <option value="{{isset($temp->product_temp_id) ? $temp->product_temp_id : ''}}">
                                    {{isset($temp->name) ? $temp->name : ''}}({{isset($temp->brand_name) ? $temp->brand_name : ''}})
                                </option>
                            @endforeach
                         @endif
                    </select>
                    <input type="text" id="sel_prod" class="sel_gift_type" >
                    <select id="sel_brand" style="display: none"  class="sel_gift_type" >
                        @if(sizeof($brands)==0)
                            <option value="">已经没有可添加适用范围</option>
                        @else
                            @foreach($brands as $brand)
                                <option value="{{isset($brand->brand_id) ? $brand->brand_id : ''}}">
                                    {{isset($brand->brand_name) ? $brand->brand_name : ''}}
                                </option>
                            @endforeach
                        @endif
                    </select>
                    <button onclick="addRang('{{isset($gift->id)?$gift->id:''}}')">添加</button>
                </div>
                <div class="table-responsive">
                    <table id="sample-table-1" class="table table-striped table-bordered table-hover">
                        <thead>
                        <tr>
                            <th >编号</th>
                            <th >赠品资源Id</th>
                            <th >赠品资源名</th>
                            <th>赠品类型</th>
                            <th>赠品优惠时间(以当前为准)</th>
                            <th>创建时间</th>
                            <th>满额度</th>
                            <th width="68px">操作区</th>
                        </tr>
                        </thead>

                        <tbody>
                        @foreach ($fav_gifts as $fav_gift)
                        <tr class="" data-tr-id="{{isset($fav_gift->id)?$fav_gift->id:''}}">
                            <td>
                                {{isset($fav_gift->id)?$fav_gift->id:''}}
                            </td>
                            <td>
                                {{isset($fav_gift->reg_id)?$fav_gift->reg_id:''}}
                            </td>
                            <td>
                                {{isset($fav_gift->res_name)?$fav_gift->res_name:''}}
                            </td>
                            <td>
                                @if(isset($fav_gift->type)&&$fav_gift->type==0)
                                 满额减
                                @elseif(isset($fav_gift->type)&&$fav_gift->type==1)
                                 产品模板
                                @elseif(isset($fav_gift->type)&&$fav_gift->type==2)
                                    产品
                                @elseif(isset($fav_gift->type)&&$fav_gift->type==3)
                                 品牌
                                @endif
                            </td>
                            <td>
                                <input class="form-control" style="cursor:pointer;width:70%;float:left;" type="text" name="date-range-picker"
                                       value="{{isset($fav_gift->begin_time)?date('m/d/Y',$fav_gift->begin_time):''}} - {{isset($fav_gift->end_time)?date('m/d/Y',$fav_gift->end_time):''}}"
                                       id="act_{{$fav_gift->id}}"/>
                                &nbsp&nbsp
                                <button onclick="updateUseTime('{{$fav_gift->id}}')">确定</button>
                            </td>
                            <td>
                                {{isset($fav_gift->created_at)?date('Y-m-d H:i:s',$fav_gift->created_at):""}}
                            </td>
                            <td style="width:68px;">
                                <input type="number" step=0.01 min=0 name="quota" value="{{isset($fav_gift->quota)?$fav_gift->quota:0}}" class="quota" style="width:68px;">
                                <input type="hidden" value="{{isset($fav_gift->quota)?$fav_gift->quota:0}}">
                                <input type="hidden" value="{{isset($fav_gift->id)?$fav_gift->id:0}}">
                            </td>
                            <td class="center" style="width:68px;">
                                <div class="visible-md visible-lg hidden-sm hidden-xs action-buttons">
                                    <a class="deletechannel" title="删除" style="color:#d77600;cursor: pointer;" onclick="deleteChannelRes({{$fav_gift->id}})">
                                        <i class="icon-trash bigger-130"></i>
                                    </a>
                                </div>
                            </td>
                        </tr>
                        @endforeach
                        </tbody>
                    </table>
                </div>
                <div class="row">
                    <div class="col-sm-6">
                        <div class="dataTables_info" id="sample-table-2_info"></div>
                    </div>
                    <div class="col-sm-6">
                        <div class="dataTables_paginate paging_bootstrap">
                            <?php echo $fav_gifts->render(); ?>
                        </div>
                    </div>
                </div>
                <!-- PAGE CONTENT ENDS -->
            </div><!-- /.col -->
        </div><!-- /.row -->
    </div><!-- /.page-content -->
@stop
@section('script')
    <script src="backstage/assets/js/date-time/bootstrap-datepicker.min.js"></script>
    <script src="backstage/assets/js/date-time/bootstrap-timepicker.min.js"></script>
    <script src="backstage/assets/js/date-time/moment.min.js"></script>
    <script src="backstage/assets/js/date-time/daterangepicker.min.js"></script>
    <script type="text/javascript">
        $(function () {
            $('input[name=date-range-picker]').daterangepicker().prev().on(ace.click_event, function () {
                $(this).next().focus();
            });
            $(".quota").blur(function(){
                var quota = $(this).next().val();
                var newQuota= $(this).val();
                var id = $(this).next().next().val();
                var a = $(this);
                if(!checkDecimals(newQuota)){
                    swal({
                        title: "金额格式不正确!!！",
                        text: "",
                        type: "error",
                        timer:2000
                    });
                }else if(quota == newQuota){
                    return;
                }else{
                    $.ajax({
                        type: "POST",
                        url: "/backstage/gift/update-quota",
                        data:{"quota":newQuota,"id":id},
                        success: function(msg){
                            if(msg.state==1){
                                swal({
                                    title: "更新成功",
                                    text: "",
                                    type: "success",
                                    timer:2000
                                });
                            }else {
                                swal({
                                    title: msg.message,
                                    text: "",
                                    type: "error",
                                    timer:2000
                                });
                            }
                        },
                        error:function(){
                            swal({
                                title: "系统繁忙！",
                                text: "",
                                type: "error",
                                timer:3000
                            });
                        }
                    });
                }
            });
            $('#sel_gift_type').change(function() {
                var gift_type=$(this).val();
                $('.sel_gift_type').hide();
                $('#'+gift_type).show();
            });
        });
        function deleteChannelRes(id) {
            swal({
                title: "确定删除？",
                text: "",
                type: "warning",
                showCancelButton: true,
                confirmButtonColor: "#DD6B55",
                confirmButtonText: "确定",
                cancelButtonText: "取消",
                closeOnConfirm: true,
                closeOnCancel: true
            },function (){
                $.ajax({
                    type: "post",
                    url: "/backstage/gift/fav-gift-delete",
                    data:{"id":id},
                    dataType: 'json',
                    success: function(res){
                        if(res.state==1){
                            swal({
                                title:'删除成功！',
                                text: "",
                                type: "success",
                                timer:2000
                            });
                            $('[data-tr-id='+id+']').remove();
                        }else{
                            swal({
                                title: res.message,
                                text: "",
                                type: "error",
                                timer:3000
                            });
                        }
                    },
                    error:function(){
                        swal({
                            title: "系统繁忙！",
                            text: "",
                            type: "error",
                            timer:2000
                        });
                    }
                });//ajax
            });
        }
        function getQueryString(name) {
            var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)", "i");
            var r = window.location.search.substr(1).match(reg);
            if (r != null) return unescape(r[2]); return null;
        }
        var state = 0
        function addRang(th){
            if(state == 0){
                state = 1;
                var gift_type = $("#sel_gift_type option:selected").val();
                switch (gift_type){
                    case 'sel_quota' :
                        resource_id='0';
                        break;
                    case 'sel_prod_temp' :
                        var resource_id = $("#sel_prod_temp option:selected").val();
                        break;
                    case 'sel_prod' :
                        var resource_id=$('#sel_prod').val();
                        break;
                    case 'sel_brand' :
                        var resource_id = $("#sel_brand option:selected").val();
                        break;
                    default : break;
                }
                if(resource_id == undefined || resource_id == ''){
                    swal('已经没有适用范围可添加了～','','warning');
                    state = 0;
                    return ;
                }
                swal('处理中，请稍后','','warning');
                $.ajax({
                    type: "post",
                    url: "/backstage/gift/add-gift-map",
                    data:{"gift_id":th,"gift_type":gift_type,"resource_id":resource_id},
                    dataType: 'json',
                    success: function(res){
                        if(res.state ==1){
                            swal('添加成功,记得填写对应满减额度使用','','success');
                            setTimeout(function(){},1000);
                            location.reload();
                            return ;
                        }
                        swal(res.message,'','warning');
                    },
                    error:function(){
                        swal('系统繁忙','请稍后重试','warning');
                    }
                });
                state = 0;
            }

        }
        /***
         * 整数格式校验
         * @param input
         * @returns {boolean}
         */
        function checkRate(input)
        {
            var reg = new RegExp("^[0-9]{1,9}$");
            return reg.test(input);
        }
        /***
         * 小数格式校验
         * @param num
         * @returns {boolean}
         */
        function checkDecimals(num)
        {
            var reg = /^\d+(?=\.{0,1}\d+$|$)/
            return reg.test(num);
        }
        function updateUseTime(th){
            var use_time	= $("#act_"+th).val();
            $.ajax({
                url:"/backstage/gift/update-gift-use-time",
                type:"post",
                data:{"id":th,"use_time":use_time},
                datatype:"json",
                success:function(res){
                    if(res.state==1){
                        swal({
                            title:'更新成功！',
                            text: "",
                            type: "success",
                            timer:1000
                        });
                        window.location.reload();
                        return;
                    }else {
                        swal({
                            title: res.message,
                            text: "",
                            type: "error",
                            timer:1000
                        });
                        return;
                    }
                }, error:function(){
                    swal({
                        title: "系统繁忙！",
                        text: "",
                        type: "error",
                        timer:2000
                    });
                }
            });
        }
    </script>
@stop